<?php
/**
 * Admin_Libraries_Controller_Action
 * @version            $Id: Action.php 01-08-2010
 * @file            Action.php
 */
abstract class Default_Libraries_Controller_Action extends Core_Controller_Action
{
    
    public function init()
    {
        parent::init(); 
        Zend_Layout::getMvcInstance()->setLayoutPath(APP_PATH.DS."modules".DS."default".DS.'layouts')
        							 ->setLayout('default');
        $this->loadBlock();
       // $this->_helper->layout()->setLayout('default');
        
    }

   
    /**
     * get IP client
     * @return String IP
     */
    function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    /**
     * @param integer $total
     * @param integer $limit
     * @return integer
     */
    public function getTotalPage($total, $limit)
    {
        return (int) ($total / $limit) + ((($total % $limit) != 0) ? 1 : 0);
    }
    
    /**
     * Config Pagination
     */
    public function configPagination ($params, $configLimit, $total)
    {
        $paginationConfig['directory'] = APP_PATH . DS . 'modules' . DS . $params['module'] . DS . 'layouts';
        $paginationConfig['items_per_page'] = $configLimit;
        $paginationConfig['total_items'] = $total;
        $paginationConfig['params'] = $params;
        $this->view->assign('pagination_config', $paginationConfig);
    }
    
    /**
     * Check permission
     * @param: string $key
     * @param: string $action
     * @return int
     */
    function checkPermission($key,$action)
    {
        $permission = $this->view->userProfile['permission'];
        if (array_key_exists($key, $permission)) {
            switch ($action) {
                case 'add':    
                    return $permission[$key][0]; 
                case 'edit':
                    return $permission[$key][1]; 
                case 'delete':
                    return $permission[$key][2]; 
                case 'view':
                    return $permission[$key][3]; 
                default:return 0;
            }
        }
        return 0;
    }
    
    /**
     * @todo load all block
     * @return void
     */
    private function loadBlock() {
    	$blocksConfig = new Zend_Config_Ini ( APP_PATH . DS . 'configs' . DS . 'blocks.ini' );
    	$blocks = $blocksConfig->toArray ();
    	foreach ( $blocks as $blockName => $options ) {
    		if ($options ['display'] == 'all') {
    			$this->_helper->block->add ( $blockName );
    		} elseif ($options ['display'] != 'none') {
    			$modules = explode ( ',', $options ['display'] );
    			for($i = 0; $i < count ( $modules ); $i ++) {
    				if ($this->_request->getModuleName () == $modules [$i]) {
    					$this->_helper->block->add ( $blockName );
    					break;
    				}
    			}
    		}
    	}
    }
}
